using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class MenuAccessor:IMenuRepository
    {
        public override bool Insert(Menu entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@MenuID","Menu_Insert",entity.MenuID,entity.GroupID,entity.Name,entity.ParentID,entity.Url,entity.IconUrl,entity.IsActive);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Menu entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_Update", entity.MenuID, entity.GroupID, entity.Name, entity.ParentID, entity.Url, entity.IconUrl, entity.IsActive, entity.SortIndex);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Menu_Delete", id) > 0;
        }
        public override bool Delete(Menu entity)
        {
            return Delete(entity.MenuID);
        }

       /*
        public override  Menu SqlDataReader(SqlDataReader reader) {
            Menu m = new Menu();
            m.MenuID = ConvertObject.ToInt(reader["MenuId"].ToString());
            m.ParentID = ConvertObject.ToInt(reader["ParentId"].ToString());
            m.Name = reader["Name"].ToString();
            m.Url = reader["Url"].ToString();
            m.IconUrl = reader["IconUrl"].ToString();
            m.SortIndex = ConvertObject.ToInt(reader["SortIndex"].ToString());
            m.IsActive = ConvertObject.ToBool(reader["IsActive"].ToString());
            m.Group = MenuGroupManager.FindById(1);
            return m;
        }
        */
        public override List<Menu> FindAll()
        {
             return CBO.FillCollection<Menu>(IDataProvider.Instance.ExecuteReader("Menu_FindAll"));
            /*
            SQLExecute sqlExe = new SQLExecute();
            List<Menu> list = new List<Menu>();
            using (SqlDataReader reader = sqlExe.Execute_DataReader("Menu_FindAll", null, CommandType.StoredProcedure))
            {
                while (reader != null && reader.Read())
                {
                    list.Insert(0, SqlDataReader(reader));
                }
            }

            return list;
           */
        }
        public override List<Menu> FindByGroup(string uniqueName)
        {
            return FindAll().FindAll(c => c.IsActive == true && c.GroupID == MenuGroupManager.FindByUniqueName(uniqueName).GroupID);
        }
        public override List<Menu> FindByGroupId(int groupId)
        {
            return FindAll().FindAll(c => c.GroupID == groupId);
        }
        public override Menu FindById(int id)
        {
            return CBO.FillObject<Menu>(IDataProvider.Instance.ExecuteReader("Menu_FindByID", id));
        }

        public override List<Menu> FindByParentId(int parentId)
        {
            return FindAll().FindAll(c => c.ParentID == parentId);
        }
        public override Menu Single(int id)
        {
            return FindById(id);
        }
        public override Menu FindByName(string name)
        {
            return FindAll().Find(c => c.Name == name);
        }
        public override List<Menu> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Menu_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Menu>(reader);
        }
        public override List<Menu> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
             var query = from c in FindAll()
                    select c;
                    totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Menu_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Menu_RestoreAll");
            return rs > 0;
        }

    }
}